package engine

type Parser interface {
	Parse(contents []byte, url string) ParseResult
	Serialize() (name string, args interface{})
}

type ParseResult struct {
	Request []Request
	Items   []Item
}

type ParserFunc func([]byte, string) ParseResult

type Request struct {
	Url    string
	Parser Parser
}

type Item struct {
	Id      string
	Type    string
	Url     string
	Payload interface{}
}

func isDuplicate(url string) bool {
	if url == "" {
		return false
	}

	return true
}

type NilParser struct{}

func (NilParser) Parse(_ []byte, _ string) ParseResult {
	return ParseResult{}
}

func (NilParser) Serialize() (_ string, _ interface{}) {
	return "NilParser", nil
}

func NilParseResult(bytes []byte) ParseResult {
	return ParseResult{}
}

type FuncParser struct {
	parser ParserFunc
	name   string //方法名
}

func (f *FuncParser) Parse(contents []byte, url string) ParseResult {
	return f.parser(contents, url)
}

func (f *FuncParser) Serialize() (name string, args interface{}) {
	return f.name, nil
}

//工厂函数去建立FuncParser
func NewFuncParser(p ParserFunc, name string) *FuncParser {
	return &FuncParser{
		name:   name,
		parser: p,
	}
}
